<template>
  <view class="apply_bank_qrcode_step3">
    <steps :active="3"></steps>
    <view class="cut-line"></view>
    <scroll-view class="scroll-wrap" :scroll-y="true" :style="{height: (safeHeight - safeWidth / 375 * (101 + 64 + 11)) + 'px'}">
      <view class="form-wrap">
        <form>
          <view class="form-item">
            <view class="form-item-head">
              <view class="title">
                法人身份证
              </view>
              <!-- <view class="sample">
                <text>示例</text>
              </view> -->
            </view>
            <view class="form-item-content" style="display: flex;">
              <view class="common-img-wrap" @click="uploadFile(1)">
                <view class="no-upload-img" v-if="!data.file1 || !data.file1Url">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传法人身份证正面
                  </view>
                </view>
                <view class="upload-img-wrap" v-else>
                  <image :src="data.file1Url" mode="" class="upload-img"></image>
                </view>
              </view>
              <view class="common-img-wrap" style="margin-left: 27rpx;" @click="uploadFile(2)">
                <view class="no-upload-img" v-if="!data.file2 || !data.file2Url">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传法人身份证反面
                  </view>
                </view>
                <view class="upload-img-wrap" v-else>
                  <image :src="data.file2Url" mode="" class="upload-img"></image>
                </view>
              </view>
            </view>
          </view>
        </form>
      </view>
      
      <view class="tip-wrap">
        <view class="head">
          <view class="title">
            上传标准
          </view>
          <view class="desc">
            <text>拍摄时请确保身份证</text>
            <text class="warning-info">边框完整，字体清晰，亮度均匀</text>
          </view>
        </view>
        <view class="content">
          <view class="sub-content">
            <image src="/static/image/qrcode/standard-1.png" mode="" class="img"></image>
            <view class="text">
              标准样式
            </view>
          </view>
          <view class="sub-content">
            <image src="/static/image/qrcode/non-standard-2.png" mode="" class="img"></image>
            <view class="text">
              拍摄不完整
            </view>
          </view>
          <view class="sub-content">
            <image src="/static/image/qrcode/non-standard-3.png" mode="" class="img"></image>
            <view class="text">
              拍摄模糊
            </view>
          </view>
          <view class="sub-content">
            <image src="/static/image/qrcode/non-standard-4.png" mode="" class="img"></image>
            <view class="text">
              闪光遮挡
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    
    
    <view class="qrcode_selected_platform-footer">
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="submitInfo" v-if="status == null || status == 0 || status == 3">下一步</button>
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="nextStep" v-else>下一步</button>
    </view>
  </view>
  
</template>

<script>
import formValid from '@/unit/graceChecker.js'
import steps from '../common/bankSteps.vue'
import { uploadFile } from '@/unit/common.js'
import { saveLegalInfoApi, getSubmitLegalInfoApi, getStepStatusApi, getIdentityInfoSyncApi } from '@/service/qrCode/qrCode.service.js'
import { discernCharterApi, discernCardApi } from '@/service/common/common.service.js'
import { setSafeAreaSize } from '@/mixin/index.mixin.js'
export default {
  mixins: [setSafeAreaSize],
  components: {
    steps
  },
  data () {
    return {
      status: null,
      data: {
        storeCollectId: null,
        file1: null, // 法人身份证正面照片Id
        file1Url: null, // 法人身份证正面照片Url
        file2: null, // 法人身份证反面照片id
        file2Url: null // 法人身份照片反面照片Url
      }
    }
  },
  methods: {
    getStepStatus () {
      getStepStatusApi({}, res => {
        if (res.status == 0) {
          this.status = res.data.status;
          this.data.storeCollectId = res.data.id;
          if (res.data.legalStatus == 1) {
            this.getInfo(res.data.id);
          } else {
            this.init();
          }
        }
      });
    },
    async init () {
      let info = await getIdentityInfoSyncApi();
      if (info) {
        this.data.legalPerson = info.name;
        this.data.cardNumber = info.num;
        this.data.cardValidity = info.endDate;
        this.data.file2 = info.fileBackId;
        this.data.file2Url = info.userBackUrl;
        this.data.file1 = info.fileFaceId;
        this.data.file1Url = info.userFaceUrl;
      }
    },
    getInfo (id) {
      getSubmitLegalInfoApi({id: id}, res => {
        if (res.status == 0) {
          this.data.legalPerson = res.data.legalPerson || null;
          // this.data.idnoType = 1;
          this.data.cardNumber = res.data.cardNumber || null;
          this.data.cardValidity = res.data.cardValidity || null;
          this.data.file2 = res.data.file2.id;
          this.data.file2Url = res.data.file2.url;
          this.data.file1 = res.data.file1.id;
          this.data.file1Url = res.data.file1.url;
          // info.businessLicenseFileId = info.businessLicenseFile.id;
          // info.businessLicenseFileUrl = info.businessLicenseFile.url;
          // delete info.businessLicenseFile;
          // this.data = info;
        }
      }, null, {showErrorMsg: true});
    },
    nextStep () {
      uni.navigateTo({
        url: '/pages/qrCode/applyBank/step4'
      }, 1500)
    },
    submitInfo () {
      let rule = [
        {name:'file1', checkType : 'notnull', checkRule:'',  errorMsg:'请上传法人身份证正面照'},
        {name:'file2', checkType : 'notnull', checkRule:'',  errorMsg:'请上传法人身份证背面照'},
      ];
      var checkRes = formValid.check(this.data, rule);
      if (checkRes) {
        let params = Object.assign(this.data);
        delete params.file1Url;
        delete params.file2Url;
        saveLegalInfoApi(this.data, res => {
          if (res.status == 0) {
            uni.showToast({
              title: '上传法人身份证成功！',
              success: () => {
                setTimeout(function () {
                  uni.navigateTo({
                    url: '/pages/qrCode/applyBank/step4'
                  }, 1500)
                })
              }
            })
          }
        }, null, {showErrorMsg: true});
      } else {
        uni.showToast({ title: formValid.error, icon: "none" });
      }
    },
    async uploadFile (type) {
      if (this.status == 1 || this.status == 2) {
        return false;
      }
      uploadFile({
        success: (res) => {
          switch (type) {
          case 1:
            discernCardApi({
              isFace: 0,
              appliedType: 2,
              url: res.data.url,
              id: res.data.id
            }, (callback) => {
              if (callback.status == 0) {
                this.data.legalPerson = callback.data.name;
                // this.data.idnoType = 1;
                this.data.cardNumber = callback.data.num;
                this.data.file1 = res.data.id;
                this.data.file1Url = res.data.url;
              } else {
                uni.showToast({
                  title: callback.msg,
                  icon: 'none'
                })
              }
            }); 
            break;
          case 2:
            discernCardApi({
              isFace: 1,
              appliedType: 2,
              url: res.data.url,
              id: res.data.id
            }, (callback) => {
              if (callback.status == 0) {
                this.data.cardValidity = callback.data.endDate;
                this.data.file2 = res.data.id;
                this.data.file2Url = res.data.url;
              } else {
                uni.showToast({
                  title: callback.msg,
                  icon: 'none'
                })
              }
            });
            break;
          default:
          }
        }
      });
    }
  },
  mounted() {
    this.getStepStatus();
  },
  onShow () {
    // 
  },
  onLoad (option) {
    // let info = await 
  }
}
</script>

<style lang="scss" scoped>
  .apply_bank_qrcode_step3 {
    min-height: calc(100vh);
    background-color: #FFFFFF;
    .cut-line {
      height: 22rpx;
      background-color: #FAFAFA;
    }
    .scroll-wrap {
      height: calc(100vh - 62px - 101px); 
    }
    .cut-line {
      height: 22rpx;
      background-color: #FAFAFA;
    }
    .form-wrap {
      // margin-top: 20rpx;
      background-color: #FFFFFF;
      padding: 0 34rpx 22rpx;
      
      .form-item {
        .form-item-head {
          overflow: hidden;
          padding-top: 40rpx;
          .title {
            float: left;
            color: #333333;
            font-size: 36rpx;
          }
          .sample {
            float: right;
            text {
              color: #FF7473;
              font-size: 28rpx;
              text-decoration: underline;
            }
          }
          &:after {
            clear: both;
          }
        }
        .form-item-content {
          margin-top: 30rpx;
          .common-img-wrap {
            width: 330rpx;
            height: 200rpx;
            border: 1px dashed #E1E1E1;
            border-radius: 16rpx;
            
            .no-upload-img {
              width: 100%;
              height: 100%;
              display: flex;
              flex-flow: column;
              align-items: center;
              justify-content: center;
              .img {
                width: 80rpx;
                height: 80rpx;
              }
              .text {
                margin-top: 29rpx;
                color: #666666;
                font-size: 24rpx;
                font-weight:500;
              }
            }
            
            .upload-img-wrap {
              width: 100%;
              height: 100%;
              
              .upload-img {
                width: 100%;
                height: 100%;
                border-radius: 16rpx;
              }
            }
          }
        }
      }  
    }
    
    .tip-wrap {
      background-color: #FFFFFF;
      padding: 40rpx 32rpx;
      margin-top: 30rpx;
      .head {
        
        .title {
          color: #333333;
          font-size: 36rpx;
        }
        .desc {
          margin-top: 19rpx;
          color: #333333;
          font-size: 24rpx;
          
          .warning-info {
            margin-left: 18rpx;
            color: #FF7473;
          }
        }
      }
      .content {
        display: flex;
        justify-content: space-between;
        margin-top: 40rpx;
        .sub-content {
          display: flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
          .img {
            width: 140rpx;
            height: 140rpx;
          }
          .text {
            // margin-top: 26rpx;
            font-size: 24rpx;
            color: #333333;
          }
        }
      }
    }
    .qrcode_selected_platform-footer {
      position: fixed;
      bottom: 0rpx;
      left: 0rpx;
      width: 100%;
      height: 125rpx;
      text-align: center;
      display: flex;
      align-items: center;
      justify-content: center;
      border-top: 1rpx solid #E1E1E1;
      background: #FFFFFF;
      .qrcode_selected_platform-footer-btn {
        width: 687rpx;
        height: 90rpx;
        line-height: 90rpx;
        background-color: #434343;
        color: #F6DA99;
        font-size: 30rpx;
      }
    }
  }
  
</style>
